package keeto.com.vn.bms.ui.activity;

import keeto.com.vn.bms.config.BMSCore;
import keeto.com.vn.bms.config.IBMSConfig;
import keeto.com.vn.bms.model.BaseInfor.DetailBMSProduct;
import keeto.com.vn.bms.model.BaseInfor.Education;
import keeto.com.vn.bms.model.BaseInfor.ProductInfor;
import keeto.com.vn.bms.model.BaseInfor.Promotion;
import keeto.com.vn.bms.model.BaseInfor.Seminar;
import keeto.com.vn.bms.network.IBMSProtocol;
import keeto.com.vn.bms.utils.BMSListener;
import keeto.com.vn.bms.utils.UniversalImageLoader;
import keeto.com.vn.bms.utils.Utils;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebView;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

public class DetailProductInfoActivity extends Activity {

	public static final String ACTION_VIEW_INFO_PRODUCT = "Product";
	public static final String ACTION_VIEW_PROMOTION = "Promotion";
	public static final String ACTION_VIEW_EDUCATION = "Education";
	public static final String ACTION_VIEW_SEMINAR = "Seminar";

	public static final String ITEM_INFO_PRODUCT = "Item_Product";
	public static final String ITEM_PROMOTION = "Item_Promotion";
	public static final String ITEM_EDUCATION = "Item_Education";
	public static final String ITEM_SEMINAR = "Item_Seminar";

	private ProductInfor mProductInfor;
	private Promotion mPromotion;
	private Education mCourses;
	private Seminar mWorkshops;
	private DetailBMSProduct mBmsProduct;

	private TextView tvTitle,tvTitleBar,tvTime,tvIntro;
	private WebView WebDesc;
	private ImageView imageView;
	private DetailBMSListener listener = new DetailBMSListener();
	private UniversalImageLoader imageLoader;
	private ImageButton btnBack;
	private String mAction;
	private RelativeLayout rlInfo;
	private LinearLayout llProduct;
	private TextView tvTitleProduct;
	private ImageView imgProduct;
	private TextView tvInfoProduct;


	public static void actionLaunchViewInfo(Context context, ProductInfor productInfor) {
		Intent intent = new Intent(context, DetailProductInfoActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(ITEM_INFO_PRODUCT, productInfor);
		intent.setAction(ACTION_VIEW_INFO_PRODUCT);
		context.startActivity(intent);
	}

	public static void actionLaunchViewPromotion(Context context, Promotion promotion) {
		Intent intent = new Intent(context, DetailProductInfoActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(ITEM_PROMOTION, promotion);
		intent.setAction(ACTION_VIEW_PROMOTION);
		context.startActivity(intent);
	}

	public static void actionLaunchViewEducation(Context context, Education courses) {
		Intent intent = new Intent(context, DetailProductInfoActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(ITEM_EDUCATION, courses);
		intent.setAction(ACTION_VIEW_EDUCATION);
		context.startActivity(intent);
	}
	public static void actionLaunchViewSeminar(Context context, Seminar seminar) {
		Intent intent = new Intent(context, DetailProductInfoActivity.class);
		intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
		intent.putExtra(ITEM_SEMINAR, seminar);
		intent.setAction(ACTION_VIEW_SEMINAR);
		context.startActivity(intent);
	}


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.detail_common_base);
		tvTitleBar=(TextView)findViewById(R.id.tv_title_name);
		btnBack = (ImageButton)findViewById(R.id.btn_tittle_back);
		imageView = (ImageView)findViewById(R.id.img_detail);
		tvTitle = (TextView) findViewById(R.id.tv_name_detail);
		tvIntro=(TextView)findViewById(R.id.tv_des_detail);
		tvTime=(TextView)findViewById(R.id.tv_time_detail);
		WebDesc = (WebView) findViewById(R.id.WebDesc);
		rlInfo = (RelativeLayout)findViewById(R.id.rl_detail_orther);
		llProduct = (LinearLayout)findViewById(R.id.ll_detail_product);
		tvTitleProduct = (TextView)findViewById(R.id.tv_name_detail_product);
		imgProduct = (ImageView)findViewById(R.id.img_detail_product);
		tvInfoProduct = (TextView)findViewById(R.id.tv_info_detail_product);

		imageLoader = new UniversalImageLoader(this);
		onNewIntent(getIntent());
		btnBack.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				DetailProductInfoActivity.this.finish();
			}
		});

	}

	@Override
	protected void onNewIntent(Intent intent) {
		// TODO Auto-generated method stub
		super.onNewIntent(intent);
		setIntent(intent);
		mAction = intent.getAction();
		Bundle bundle = intent.getExtras();

		if (mAction.equals(ACTION_VIEW_INFO_PRODUCT) && bundle != null){
			mProductInfor = (ProductInfor) bundle.get(ITEM_INFO_PRODUCT);
			if (mProductInfor != null){
				BMSCore.getInstance(this).taskGetDetailBMSProduct(mProductInfor.getID(),IBMSConfig.KEY_TYPE_DETAIL_PRODUCT, listener);
			}
			rlInfo.setVisibility(View.GONE);
			llProduct.setVisibility(View.VISIBLE);
		}else if (mAction.equals(ACTION_VIEW_PROMOTION) && bundle != null){
			mPromotion = (Promotion) bundle.get(ITEM_PROMOTION);
			if (mPromotion != null){
				BMSCore.getInstance(this).taskGetDetailBMSProduct(mPromotion.getID(),IBMSConfig.KEY_TYPE_DETAIL_PROMOTION, listener);
			}
			rlInfo.setVisibility(View.VISIBLE);
			llProduct.setVisibility(View.GONE);
		}else if (mAction.equals(ACTION_VIEW_EDUCATION) && bundle!= null) {
			mCourses = (Education) bundle.get(ITEM_EDUCATION);
			if (mCourses != null){
				BMSCore.getInstance(this).taskGetDetailBMSProduct(mCourses.getID(),IBMSConfig.KEY_TYPE_DETAIL_EDUCATION, listener);
			}
			rlInfo.setVisibility(View.VISIBLE);
			llProduct.setVisibility(View.GONE);
		}else if (mAction.equals(ACTION_VIEW_SEMINAR) && bundle!= null){
			mWorkshops = (Seminar) bundle.get(ITEM_SEMINAR);
			if (mWorkshops != null){
				BMSCore.getInstance(this).taskGetDetailBMSProduct(mWorkshops.getID(),IBMSConfig.KEY_TYPE_DETAIL_SEMINAR, listener);
			}
			rlInfo.setVisibility(View.VISIBLE);
			llProduct.setVisibility(View.GONE);

		}


	}

	class DetailBMSListener extends BMSListener{
		@Override
		public void finishGetDetailBMSProduct(final int returnCode,
				final DetailBMSProduct product) {
			// TODO Auto-generated method stub
			super.finishGetDetailBMSProduct(returnCode, product);
			runOnUiThread(new Runnable() {

				@Override
				public void run() {
					// TODO Auto-generated method stub
					if(returnCode==IBMSProtocol.STATUS_CODE_OK){
						mBmsProduct = product;

						if (mAction.equals(ACTION_VIEW_INFO_PRODUCT)){
							tvTime.setText("");
							tvTitleProduct.setText(mBmsProduct.getTitle());
							tvTitleBar.setText(mBmsProduct.getTitle());
							tvInfoProduct.setText(mBmsProduct.getIntro());
							imageLoader.displayImageView(mBmsProduct.getImage(),imgProduct);
						}else{
							tvTitle.setText(mBmsProduct.getTitle());
							tvTitleBar.setText(mBmsProduct.getTitle());
							tvIntro.setText(mBmsProduct.getIntro());
							imageLoader.displayImageView(mBmsProduct.getImage(),imageView);
							if (mAction.equals(ACTION_VIEW_PROMOTION)){
								tvTime.setText(DetailProductInfoActivity.this.getResources().getString(R.string.tv_time_promotion)+ Utils.convertTimePromotion(mBmsProduct.getDurationFrom()));
							}else if (mAction.equals(ACTION_VIEW_EDUCATION)){
								tvTime.setText(DetailProductInfoActivity.this.getResources().getString(R.string.tv_time_education)+ Utils.convertTimePromotion(mBmsProduct.getDurationFrom()));
							}
						}

						WebDesc.loadDataWithBaseURL(null, mBmsProduct.getDesc(),"text/html", "utf-8", null);

					}else if(returnCode==IBMSProtocol.STATUS_CODE_EXIT_RECORD){
						Toast.makeText(DetailProductInfoActivity.this,DetailProductInfoActivity.this.getResources().getString(R.string.error_data_server) , Toast.LENGTH_SHORT).show();
					} else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL_CONECTTION){
						Toast.makeText(DetailProductInfoActivity.this,DetailProductInfoActivity.this.getResources().getString(R.string.error_network_connection) , Toast.LENGTH_SHORT).show();
					} else if(returnCode==IBMSProtocol.STATUS_CODE_GENERIC_FAIL){
						Toast.makeText(DetailProductInfoActivity.this,DetailProductInfoActivity.this.getResources().getString(R.string.error_perpose) , Toast.LENGTH_SHORT).show();
					}
					
					

				}
			});

		}
	}

}
